package cn.rxmind.dlicense.core.creator;

import cn.rxmind.dlicense.core.base.LicenseManagerWrapper;
import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.factory.DLicenseContentFactory;
import cn.rxmind.dlicense.core.factory.DLicenseParamFactory;
import cn.rxmind.dlicense.core.exception.DLicenseErrEnum;
import cn.rxmind.dlicense.core.exception.DLicenseExceptionFactory;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseParam;

/**
 * 服务端证书管理器，主要用来生成证书
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/12 1:47 下午
 */
public class DServerLicenseManager implements DLicenseCreator {

    private final LicenseManagerWrapper licenseManagerWrapper;

    public DServerLicenseManager() {

        LicenseParam licenseParam = DLicenseParamFactory.createLicenseParam4Server();
        this.licenseManagerWrapper = new LicenseManagerWrapper(licenseParam,null);
    }

    /**
     * 生成证书
     * @param content
     * @return
     */
    @Override
    public byte[] createLicense(DLicenseContent content) {

        LicenseContent licenseContent = DLicenseContentFactory.createLicenseContent(content);
        try{
            return licenseManagerWrapper.create(licenseContent);

        }catch (Exception e){
            e.printStackTrace();
            throw DLicenseExceptionFactory.buildUtryLicenseException(DLicenseErrEnum.LICENSE_CREATE_ERROR);
        }
    }

}
